
package fileby.ctrl.tabs;

import fileby.core.IDisplayableFile;
import fileby.model.IFileManager;
import fileby.model.ISelectionManager;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JComponent;
import javax.swing.JPopupMenu;
import javax.swing.TransferHandler;

/**
 *
 * @author Ludwig
 */
public class TabItemListener extends MouseAdapter {

    private IFileManager fileManager;
    private ISelectionManager selectionManager;
    private IDisplayableFile file;
    private JPopupMenu popup;
    private int latestMouseClick = MouseEvent.BUTTON1;
    private Point startPoint;
    private boolean drag;

    public TabItemListener(IDisplayableFile file, JPopupMenu popup,
            IFileManager fileManager, ISelectionManager selectionManager) {

        this.file = file;
        this.popup = popup;
        this.fileManager = fileManager;
        this.selectionManager = selectionManager;
    }

    @Override
    public void mousePressed(MouseEvent e) {
        drag = false;
        latestMouseClick = e.getButton();
        if (e.isPopupTrigger())
            showPopup(e);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (e.isPopupTrigger())
            showPopup(e);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        if (latestMouseClick == MouseEvent.BUTTON1) {
            if (!drag)
                startPoint = e.getPoint();
            drag = true;
            JComponent c = (JComponent) e.getSource();
            if (e.isControlDown())
                selectionManager.setSelected(file.getAbsolutePath(), true);
            else if (!file.isSelected())
                selectionManager.setSingleSelected(file.getAbsolutePath());
            if (startPoint.distance(e.getPoint()) > 15) {
                TransferHandler handler = c.getTransferHandler();
                if (e.isControlDown()) {
                    handler.exportAsDrag(c, e, TransferHandler.COPY);
                } else {
                    handler.exportAsDrag(c, e, TransferHandler.MOVE);
                }
            }
        }
        drag = true;
    }


    @Override
    public void mouseClicked(MouseEvent e) {
        drag = false;
        ((JComponent)e.getSource()).requestFocusInWindow();
        latestMouseClick = e.getButton();
        // Single click on a file
        if (e.getButton() == MouseEvent.BUTTON1) {
            // Single click with left button
            if (e.getClickCount() == 1) {
                if (e.isControlDown()) // Ctrl down
                    selectionManager.setSelected(file.getAbsolutePath(), !file.isSelected());
                else
                    selectionManager.setSingleSelected(file.getAbsolutePath());
            } else if (e.getClickCount() == 2) {
                // Double click
                if (e.isAltDown()) // Alt down
                    fileManager.open(false);
                else
                    fileManager.open(true);
            }
        } else if (e.getButton() == MouseEvent.BUTTON2) {
            fileManager.open(false);
        } else if (e.getButton() == MouseEvent.BUTTON3) {
            showPopup(e);
        }
    }

    private void showPopup(MouseEvent e) {
        if (!drag) {
            if (!file.isSelected())
                selectionManager.setSingleSelected(file.getAbsolutePath());
            popup.show(e.getComponent(), e.getX(), e.getY());
        }
    }

}